package com.example.controller;

import com.example.result.ReturnCode;
import com.example.result.ReturnResult;
import com.example.service.IClassificationManagementService;
import com.example.vo.ClassificationCheckUniqueParamsVO;
import com.example.vo.ClassificationCreateFormVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/classification")
public class ClassificationManagementController {
    @Autowired
    private IClassificationManagementService classificationManagementService;

    /**
     *
     * @param nameOrCode 接收分类中文名或者分类编码（CNxxxx) 如果不提供的话则默认查全量
     * @return
     */
    @GetMapping("/query")
    public ReturnResult queryByParams(@RequestParam(required = false,value = "nameOrCode") String nameOrCode){
        ReturnResult returnResult = classificationManagementService.queryByParams(nameOrCode);
        return returnResult;
    }

    /**
     *
     * @param id 分类id
     * @return 分类的父分类以及全部属性
     */
    @GetMapping("/query/details")
    public ReturnResult queryDetailsById(@RequestParam String id){
        Long id_ = Long.valueOf(id);
        ReturnResult returnResult = classificationManagementService.queryDetailsById(id_);
        return returnResult;
    }
    /*创建分类：
    * 前端传值：中英文名，中英文描述，父分类，属性
    * 涉及功能： 检验中英文名是否唯一，
    *          提交分类基本信息创建表单
    *           查询可选属性
    *           提交分类属性表单 */
    @PostMapping("/checkUnique")
    public ReturnResult checkUnique(@RequestBody ClassificationCheckUniqueParamsVO vo){
        return classificationManagementService.checkUnique(vo);
    }
    @PostMapping("/create")
    public ReturnResult create(@RequestBody ClassificationCreateFormVO vo){
        ReturnResult classificationNode = classificationManagementService.createClassificationNode(vo);
        ReturnResult attrs = classificationManagementService.addClassificationAttrs(vo.getAttrs(),(Long)classificationNode.getData());
        return new ReturnResult(ReturnCode.INSERT_OK,"创建分类成功",classificationNode.getData());
    }
    @GetMapping("/findOptionalAttrs")
    public ReturnResult findOptionalAttrs(){
        return classificationManagementService.findOptionalAttrs();
    }
}
